package com.healthcare;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.Articles;
import beans.Posts;

/**
 * Servlet implementation class SearchPosts
 */
public class SearchPosts extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchPosts() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String query = request.getParameter("query");
		System.out.print("state" +query);
	
		    		        int page = 1;
		        int recordsPerPage = 3;
		      //boolean isEmptyList = false;
		        if(request.getParameter("page") != null)
		            page = Integer.parseInt(request.getParameter("page"));
		        DAO dao = new DAO();
		        System.out.println("hello");
		        List<Posts> list = dao.viewPosts((page-1)*recordsPerPage,
		                                 recordsPerPage, query);
		        int noOfRecords = dao.getNoOfRecords();
		        int noOfPages = (int) Math.ceil(noOfRecords * 1.0 / recordsPerPage);
		        request.setAttribute("articleList", list);
		        request.setAttribute("isEmptyList", "true");
		        request.setAttribute("noOfPages", noOfPages);
		        request.setAttribute("currentPage", page);
		        RequestDispatcher view = request.getRequestDispatcher("/SearchPosts.jsp");
		        view.forward(request, response);
		 
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
